package xuecheng.tool.nosql.mongo.service.impl;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.repository.MongoRepository;

import lombok.extern.slf4j.Slf4j;
import xuecheng.tool.nosql.mongo.service.BaseMongoService;

/**
 * mongo服务基类实现
 *
 * @author PengXueCheng
 * @version 1.0.0
 **/
@Slf4j
public abstract class BaseMongoServiceImpl<T, ID, M extends MongoRepository<T, ID>>
    implements BaseMongoService<T, ID, M> {

    @Autowired
    protected MongoTemplate mongoTemplate;
    @Autowired
    protected M repo;

    @Override
    public <S extends T> List<S> saveAll(Iterable<S> entities) {
        return repo.saveAll(entities);
    }

    @Override
    public List<T> findAll() {
        return repo.findAll();
    }

    @Override
    public List<T> findAll(Sort sort) {
        return repo.findAll(sort);
    }

    @Override
    public <S extends T> S insert(S entity) {
        return repo.insert(entity);
    }

    @Override
    public <S extends T> List<S> insert(Iterable<S> entities) {
        return repo.insert(entities);
    }

    @Override
    public <S extends T> List<S> findAll(Example<S> example) {
        return repo.findAll(example);
    }

    @Override
    public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
        return repo.findAll(example, sort);
    }

    @Override
    public Page<T> findAll(Pageable pageable) {
        return repo.findAll(pageable);
    }

    @Override
    public <S extends T> S save(S s) {
        return repo.save(s);
    }

    @Override
    public Optional<T> findById(ID id) {
        return repo.findById(id);
    }

    @Override
    public boolean existsById(ID id) {
        return repo.existsById(id);
    }

    @Override
    public Iterable<T> findAllById(Iterable<ID> iterable) {
        return repo.findAllById(iterable);
    }

    @Override
    public long count() {
        return repo.count();
    }

    @Override
    public void deleteById(ID id) {
        repo.deleteById(id);
    }

    @Override
    public void delete(T t) {
        repo.delete(t);
    }

    @Override
    public void deleteAll(Iterable<? extends T> iterable) {
        repo.deleteAll(iterable);
    }

    @Override
    public void deleteAll() {
        repo.deleteAll();
    }

    @Override
    public <S extends T> Optional<S> findOne(Example<S> example) {
        return repo.findOne(example);
    }

    @Override
    public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
        return repo.findAll(example, pageable);
    }

    @Override
    public <S extends T> long count(Example<S> example) {
        return repo.count(example);
    }

    @Override
    public <S extends T> boolean exists(Example<S> example) {
        return repo.exists(example);
    }
}
